package District;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;

public class PriceByDistrictMapper  extends Mapper<LongWritable, Text, Text, DoubleWritable> {

    private static final int STRUCTURE_INDEX = 1;
    private static final int PRICE_INDEX = 14;

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] fields = line.split(",");

        if (fields.length > Math.max(STRUCTURE_INDEX, PRICE_INDEX)) {
            String structure = fields[STRUCTURE_INDEX].trim();
            try {
                double pricePerSquareMeter = Double.parseDouble(fields[PRICE_INDEX].trim());
                context.write(new Text(structure), new DoubleWritable(pricePerSquareMeter));
            } catch (NumberFormatException e) {
                System.err.println("解析数据时出错，位于行数为: " + line);
            }
        }
    }
}
